from 类型 import *

# This is a proof of concept tool to manipulate symbolic expressions representing orders of magnitude, and to prove statements about them.  Most likely it will be refactored into a more systematic formalism, so this code should be viewed as an experimental prototype rather than as a final product.


# Factory function (this will shadow Python's built‑in max in this module)
def max(*args):
    # Assuming all args are Expression instances
    return Max(*args)

def min(*args):
    # Assuming all args are Expression instances
    return Min(*args)

# TODO: add support for products of any finite number of terms (not just two)

# list all the variables found in an expression
def variables(expr):
    """Extracts all variables from an expression."""
    if isinstance(expr, 变量):
        return {expr.name}
    elif isinstance(expr, (Add, Mul, Div)):
        vars_left = variables(expr.left)
        vars_right = variables(expr.right)
        return vars_left.union(vars_right)
    elif isinstance(expr, Power):
        return variables(expr.base)  # ignore exponents for now
    elif isinstance(expr, Max) or isinstance(expr, Min):
        vars_operands = set()
        for operand in expr.operands:
            vars_operands.update(variables(operand))
        return vars_operands
    elif isinstance(expr, Statement):
        vars_left = variables(expr.left)
        vars_right = variables(expr.right)
        return vars_left.union(vars_right)
    else:
        return set()  # No variables found in this expression

# list all the expressions in a set of statements
def expressions(statements):
    """Extracts all expressions from a set of statements."""
    exprs = set()
    for statement in statements:
        exprs.add(statement.left)
        exprs.add(statement.right)
    return exprs
